package com.reservation.repository;

import com.reservation.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 商品数据访问接口
 */
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {

    /**
     * 查找所有激活的商品
     */
    List<Product> findByIsActiveTrueOrderByIdAsc();

    /**
     * 查找有库存的商品
     */
    @Query("SELECT p FROM Product p WHERE p.isActive = true AND p.stock > 0 ORDER BY p.id ASC")
    List<Product> findAvailableProducts();

    /**
     * 根据ID查找激活的商品
     */
    Optional<Product> findByIdAndIsActiveTrue(Long id);

    /**
     * 根据名称模糊查询商品
     */
    List<Product> findByNameContainingIgnoreCaseAndIsActiveTrueOrderByIdAsc(String name);

    /**
     * 查找库存不足的商品
     */
    @Query("SELECT p FROM Product p WHERE p.isActive = true AND p.stock <= :threshold ORDER BY p.stock ASC")
    List<Product> findLowStockProducts(Integer threshold);
}
